package com.doubi.controller.common;

import cn.hutool.core.util.IdUtil;
import com.doubi.AliyunOssUtil;
import com.doubi.utils.DataUtil;
import com.doubi.vo.AppModel;
import com.doubi.vo.AppUiModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

@RestController
@RequestMapping("common")
public class UploadController {
    @Autowired
    private AliyunOssUtil aliyunOssUtil;

    /**
     * 文件上传
     */
    @RequestMapping("/uploadFile")
    public AppModel uploadFile(MultipartFile file) {
        try {
            String fileName = file.getOriginalFilename();
            /*
             * 修改文件名称并保存文件
             */
            if (DataUtil.isEmpty(fileName)) {
                return AppModel.error();
            }
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            String uuid = IdUtil.simpleUUID();
            fileName = uuid + suffix;
            /*
             * 入阿里云
             */
            String finalFileName = fileName;
            return AppModel.success(0, "上传成功！",new HashMap<String,String>(){{
                put("src", aliyunOssUtil.upload(file.getInputStream(), finalFileName));
            }});
        } catch (Exception e) {
            return AppModel.error();
        }
    }


    /**
     * MrDoubi
     * 2019年1月7日 10:49:21
     * 删除文件
     * @param imgpath 图片路径 有视频的时候这个是封面路径
     * @param filepath 视频路径
     * @return 假装删了
     */
    @RequestMapping("delFile")
    @ResponseBody
    public AppModel delFile(String imgpath, String filepath){
        return new AppModel(0,"删除成功！","");
    }
}
